package com.service.impl;

import com.constant.Constant;
import com.dao.CourseMapper;
import com.entity.Course;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageHelper;
import com.service.CourseSrevice;
import com.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * date:2021/5/6
 * description:
 * Author:
 */
@Service
public class CourseSreviceImpl implements CourseSrevice {
    @Autowired
    CourseMapper courseMapper;

    //@李奇洲
    @Override
    //查询当前课程下前三的课程
    public List<Course> findTop3CoursesByCourseId(Integer courseId) {
        return courseMapper.selectTop3CoursesByCourseId(courseId);
    }
    //    根据CourseTypeID查询所有课程    李奇洲
    @Override
    public PageInfo<Course> selectAllCourseByTypeID(Integer pageNo, Integer course_type_id, HttpSession session) {
        PageHelper.startPage(pageNo, 4);
        String course_name = (String) session.getAttribute("course_name");
        if(course_name ==null){
            List<Course> courses = courseMapper.selectAllCourseByTypeID(course_type_id);
            if (null == courses) {
                throw new ServiceException("没有更多课程了...");
            }
            return new PageInfo<Course>(courses);
        }
        List<Course> courses = courseMapper.selectCourseByName(course_name);
        session.setAttribute("course_name",null);
        return new PageInfo<Course>(courses);
    }





    //  吴彬彬  条件查询
    @Override
    public PageInfo<CourseVo> findCourse(CourseVo courseVo) {

        PageHelper.startPage(courseVo.getPageNo(), Constant.PAGE_SIZE);

        System.out.println("page "+  courseVo.getPageNo());

        List<CourseVo> list = courseMapper.findCourse(courseVo);

        return new PageInfo<CourseVo>(list);

    }

    //吴彬彬  修改状态
    @Override
    public int toggle(Integer id, Integer status) {
        int i = 0;
        try{
          i = courseMapper.toggle(id, status);
        }catch (Exception e){
            e.printStackTrace();
        }
       return  i;
    }

    //吴彬彬  显示修改前信息
    @Override
    public Course show(Integer id) {

        Course show = courseMapper.show(id);

        return  show;
    }


    //吴彬彬  增加
    @Override
    public int addCourse(Course course) {
        List<Course> courses = courseMapper.selectAllCourse();
        int i =0;
        for(Course x:courses){
            if(!x.getCourse_name().equals(course.getCourse_name())){
                 i = courseMapper.addCourse(course);
                break;
            }
        }

        return  i;

    }
    //吴彬彬  修改
    @Override
    public int modifyCourse(Course course) {

        List<Course> courses = courseMapper.selectAllCourse();
        int i =0;
        for(Course x:courses){
            if(!x.getCourse_name().equals(course.getCourse_name())){
                i = courseMapper.modifyCourse(course);
                break;
            }
        }

        return  i;
    }

    @Override
    public List<Course> findTop15() {
            List<Course> courseList = courseMapper.selectTop15();
            return courseList;
    }
}
